#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>

#include "head.h"

int hass_fd, qt_fd, acceptfd;

void *thread(void *arg) //线程作为hass的客户端
{
    int recvbyte;
    MSG_t recv_msg;
    if (connect(hass_fd, (struct sockaddr *)arg, sizeof(struct sockaddr_in)) < 0)
    {
        perror("connect error.");
        return NULL;
    }

    while (1)
    {
        recvbyte = recv(hass_fd, &recv_msg, sizeof(recv_msg), 0);
        if (recvbyte < 0)
        {
            perror("recv error.");
            // return NULL;
        }
        printf("hass-recv:%s %d %d %f %s\n", recv_msg.name, recv_msg.type, recv_msg.ival, recv_msg.fval, recv_msg.text);
        send(acceptfd, &recv_msg, sizeof(recv_msg), 0);
    }
    pthread_exit(NULL);
}

int server(int argc, char const *argv[])
{
    MSG_t msg;

    if (argc != 4)
    {
        printf("please input %s <hassserver_ip> <hassserver_port> <QTclient_port>\n", argv[0]);
        return -1;
    }

    //1.创建等待QT端通信服务器

    qt_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (qt_fd < 0)
    {
        perror("socket qt_fd error.");
        return -1;
    }

    //2.填充结构体
    struct sockaddr_in qtServeraddr, qtClientaddr;
    //1》填充QT通信server端结构体
    qtServeraddr.sin_family = AF_INET;
    qtServeraddr.sin_port = htons(atoi(argv[3]));
    qtServeraddr.sin_addr.s_addr = inet_addr("0.0.0.0");

    socklen_t len = sizeof(struct sockaddr_in);

    //3-1.绑定QT server端
    if (bind(qt_fd, (const struct sockaddr *)&qtServeraddr, len) < 0)
    {
        perror("bind QT_server error.");
        return -1;
    }
    //3-2.监听
    if (listen(qt_fd, 5) < 0)
    {
        perror("listen QT error.");
        return -1;
    }

    //1.创建hass客户端通信
    hass_fd = socket(AF_INET, SOCK_STREAM, 0);
    if (hass_fd < 0)
    {
        perror("socket hass_fd error.");
        return -1;
    }

    struct sockaddr_in hassServeraddr;
    //2.填充QT通信server端结构体
    hassServeraddr.sin_family = AF_INET;
    hassServeraddr.sin_port = htons(atoi(argv[2]));
    hassServeraddr.sin_addr.s_addr = inet_addr(argv[1]);
    pthread_t tid;
    pthread_create(&tid, NULL, thread, &hassServeraddr);
    pthread_detach(tid);

    int recvbyte;

    while (1)
    {
        //3-3.阻塞等待QT客户端连接
        acceptfd = accept(qt_fd, (struct sockaddr *)&qtServeraddr, &len);
        if (acceptfd < 0)
        {
            perror("accept error.");
            return -1;
        }
        printf("QT_clietn- ip:%s port:%d\n", (char *)inet_ntoa(qtClientaddr.sin_addr), ntohs(qtClientaddr.sin_port));
        while (1)
        {
            recvbyte = recv(acceptfd, &msg, sizeof(msg), 0);
            if (recvbyte < 0)
            {
                perror("recv error.");
                return -1;
            }
            else if (recvbyte == 0)
            {
                printf("QT client exit.\n");
                break;
            }
            else
            {
                printf("recv_QT:%s %d %d %f %s\n", msg.name, msg.type, msg.ival, msg.fval, msg.text);
                send(hass_fd, &msg, sizeof(msg), 0);
            }
        }
    }

    close(hass_fd);
    close(qt_fd);
    close(acceptfd);
}
